package cn.edu.neusoft.yuejian.frgment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import cn.edu.neusoft.yuejian.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class SyFragment extends Fragment implements View.OnClickListener {
    private TextView sy_hd, sy_ds,sy_bs;
    private HdFragment HdFragment;
    private BsFragment BsFragment;
    private DsFragment DsFragment;
    private View view = null;
    public SyFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        view=inflater.inflate(R.layout.fragment_sy, container, false);
        initViews();
        setSelect(0);
        initEvent();
     return view;
}

    private void initViews(){
        sy_hd = (TextView)view.findViewById(R.id.sy_hd);
        sy_ds = (TextView)view.findViewById(R.id.sy_ds);
        sy_bs = (TextView)view.findViewById(R.id.sy_bs);
        sy_hd.setSelected(true);
        sy_hd.setTextColor(getResources().getColor(R.color.colorAccent));
        sy_ds.setTextColor(getResources().getColor(R.color.colorPrimary));
        sy_bs.setTextColor(getResources().getColor(R.color.colorPrimary));
    }

    private void setSelect(int i)
    {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (HdFragment == null)
                {
                    HdFragment = new HdFragment();
                    transaction.add(R.id.fragment_container, HdFragment);
                } else
                {
                    transaction.show(HdFragment);
                }
                sy_hd.setSelected(true);
                sy_ds.setSelected(false);
                sy_bs.setSelected(false);
                sy_hd.setTextColor(getResources().getColor(R.color.colorAccent));
                sy_ds.setTextColor(getResources().getColor(R.color.colorPrimary));
                sy_bs.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;
            case 1:
                if (DsFragment == null)
                {
                    DsFragment = new DsFragment();
                    transaction.add(R.id.fragment_container, DsFragment);
                } else
                {
                    transaction.show(DsFragment);

                }
                sy_bs.setSelected(false);
                sy_hd.setSelected(false);
                sy_ds.setSelected(true);
                sy_ds.setTextColor(getResources().getColor(R.color.colorAccent));
                sy_hd.setTextColor(getResources().getColor(R.color.colorPrimary));
                sy_bs.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;
            case 2:
                if (BsFragment == null)
                {
                    BsFragment = new BsFragment();
                    transaction.add(R.id.fragment_container, BsFragment);
                } else
                {
                    transaction.show(BsFragment);

                }
                sy_ds.setSelected(false);
                sy_hd.setSelected(false);
                sy_bs.setSelected(true);
                sy_bs.setTextColor(getResources().getColor(R.color.colorAccent));
                sy_hd.setTextColor(getResources().getColor(R.color.colorPrimary));
                sy_ds.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;


            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (HdFragment != null)
        {
            transaction.hide(HdFragment);
        }
        if (DsFragment != null)
        {
            transaction.hide(DsFragment);
        }
        if (BsFragment != null)
        {
            transaction.hide(BsFragment);
        }
    }
    private void initEvent()
    {
        sy_hd.setOnClickListener(this);
        sy_ds.setOnClickListener(this);
        sy_bs.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.sy_hd:
                setSelect(0);
                break;
            case R.id.sy_ds:
                setSelect(1);
                break;
            case R.id.sy_bs:
                setSelect(2);
                break;
            default:
                break;
        }
    }
}
